package com.xjnt.base.support.admin.controller;

import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.xjnt.base.support.admin.entity.Account;
import com.xjnt.base.support.admin.entity.Org;
import com.xjnt.base.support.admin.service.OrgService;
import com.xjnt.frameworks.annotation.AutoInject;
import com.xjnt.frameworks.annotation.Router;
import com.xjnt.frameworks.core.CRUDController;
import com.xjnt.frameworks.exception.BusinessException;
import com.xjnt.frameworks.web.message.RetDataMsg;

@Router(name="/system/admin/org", view="/admin/org")
public class OrgController extends CRUDController<Org, OrgService> {
	
	@AutoInject
	private OrgService orgService;
	
	@Override
	protected String getBizName() {
		return "机构";
	}
	
	@Override
	protected Class<Org> getClazz() {
		return Org.class;
	}

	@Override
	protected OrgService getService() {
		return orgService;
	}

	public void tree(){
		RetDataMsg dataMsg = new RetDataMsg(true);
		try{
			List<Map<String, Object>> lstTree = Lists.newArrayList();
			String parentId = getPara("uuid");
			if(StrKit.isBlank(parentId)){
				parentId = null;
			}
			String level = getPara("alevel");
			if(StrKit.isBlank(level)){
				level = null;
			}
			List<Record> lstOrg = null;
			if(null == parentId && null == level){ //初始化根目录
				Account account = (Account) getCurrUser();
				if(account.getInt(Account.SUPER) == 1){ // 除超级管理员外其他任何都只能看到自己的所属机构，及下级。
					lstOrg = getService().findByParentId(parentId);
					Map<String, Object> root = Maps.newHashMap();
					root.put("name", "所有机构");
					root.put("alevel", -1);
					root.put("nocheck", true);
					if(lstOrg.size() > 0){
						root.put("isParent", "true");
						root.put("open", true);
					} else {
						root.put("isParent", "false");
					}
					lstTree.add(root);
				} else {
					String orgId = account.getStr(Account.FK_ORGID);
					if(orgId != null){
						Org org = orgService.findById(orgId);
						lstOrg = Lists.newArrayList();
						Record record = org.toRecord();
						record.set("children_count", new Long(org.getChildren().size()));
						lstOrg.add(record);
					}
				}
			} else {
				lstOrg = getService().findByParentId(parentId);
			}
				
			if(null != lstOrg && !lstOrg.isEmpty()){
				dataMsg.pushOk("机构数据已获取成功");
				dataMsg.setTotal(lstOrg.size());
				for (Record record : lstOrg) {
					Map<String, Object> children = Maps.newHashMap();
					children.put("uuid", record.getStr(Org.PK_ID));
					children.put("parentId", record.getStr(Org.FK_PID) == null ? null : record.getStr(Org.FK_PID));
					children.put("name", record.getStr(Org.NAME));
					children.put("alevel", record.getInt(Org.LEVEL));
					if(record.getLong("children_count") > 0){
						children.put("isParent", "true");
					} else {
						children.put("isParent", "false");
					}
					lstTree.add(children);
				}
			}else{
				dataMsg.pushOk("没有查询到机构数据");
			}
			dataMsg.setRows(lstTree);
			renderJson(dataMsg);
		} catch (Exception ex){
			throw new BusinessException("查询" + getBizName(), "发生异常：", ex);
		}
	}
}
